package com.hqyj.finance.server.users.financeusers.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("code")
@Controller
public class RandomCodeController extends BaseController {

    //@Resource
    //private MyCommonsUtils myCommonsUtils = new MyCommonsUtils();
    //@Resource
    //private MyCommonsUtils myCommonsUtils;

    @GetMapping("randomCodeImg")
    public void generateRadomCodeImageByHutool(HttpServletRequest request, HttpServletResponse response,String token){
        try{
           //myCommonsUtils.generateRandomCodeImage(response,token);
            CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(140, 60, 4, 20);
            //CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
            //图形验证码写出，可以写出到文件，也可以写出到流
            //captcha.write("d:/circle.png");
            //验证图形验证码的有效性，返回boolean值
            //captcha.verify("1234");
            String randomCode =  captcha.getCode(); //每次自动生成的验证码

            redisUtil.set(token,randomCode,5); //让这个验证码5分钟后自动过期。
            captcha.write(response.getOutputStream());
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
